<?php
require_once dirname(__FILE__).'/conectar.inc.php';

function ORMgetMenu($precendecia) {
    $resultado = array();
    
    $sql = "Select * from usuarios u, roles r, roles_menu rm, menu m WHERE u.idUsuario = '".$_SESSION['SID']."' AND u.rol = r.idRol AND r.idRol = rm.idRol AND rm.idMenu = m.idMenu AND m.predecesor = '$precendecia' ORDER BY m.orden";
    //$sql = "Select * from Usuarios u, Roles r, Roles_Menu rm, Menu m WHERE u.idUsuario = '1' AND u.rol = r.idRol AND r.idRol = rm.idRol AND rm.idMenu = m.idMenu AND m.predecesor = '$precendecia'";
    
    $r = mysql_query($sql) or die(mysql_error());
    while($arr = mysql_fetch_assoc($r)) {
        $resultado[$arr['idMenu']] = $arr['menu'];
    }
    
    return $resultado;
}

function ORMgetCantidad($precedencia) {
    $sql = "Select count(*) as cant from usuarios u, roles r, roles_menu rm, menu m WHERE u.idUsuario = '".$_SESSION['SID']."' AND u.rol = r.idRol AND r.idRol = rm.idRol AND rm.idMenu = m.idMenu AND m.predecesor = '$precedencia'";
    //$sql = "Select count(*) as cant from Usuarios u, Roles r, Roles_Menu rm, Menu m WHERE u.idUsuario = '1' AND u.rol = r.idRol AND r.idRol = rm.idRol AND rm.idMenu = m.idMenu AND m.predecesor = '$precedencia'";
    $r = mysql_query($sql) or die(mysql_error());
    $arr = mysql_fetch_assoc($r);
    
    return $arr['cant'];
}

function ORMgetHref($id) {
    $sql = "SELECT href FROM menu WHERE idMenu = '".$id."'";
    $r = mysql_query($sql) or die(mysql_error());
    $arr = mysql_fetch_assoc($r);
    
    return $arr['href'];
}

function ORMgetMenuCompleto($precedencia) {
    $resultado = array();
    
    $sql = "Select * from menu WHERE predecesor = '".$precedencia."' ORDER BY orden";
    $r = mysql_query($sql) or die(mysql_error());
    while($arr = mysql_fetch_assoc($r)) {
        $resultado[$arr['idMenu']] = $arr['menu'];
    }
    
    return $resultado;
}

function ORMgetCantidadMenuCompleto($precedencia) {
    $sql = "Select count(*) as cant from usuarios u, roles r, roles_menu rm, menu m WHERE u.rol = r.idRol AND r.idRol = rm.idRol AND rm.idMenu = m.idMenu AND m.predecesor = '$precedencia'";
    //$sql = "Select count(*) as cant from Usuarios u, Roles r, Roles_Menu rm, Menu m WHERE u.idUsuario = '1' AND u.rol = r.idRol AND r.idRol = rm.idRol AND rm.idMenu = m.idMenu AND m.predecesor = '$precedencia'";
    $r = mysql_query($sql) or die(mysql_error());
    $arr = mysql_fetch_assoc($r);
    
    return $arr['cant'];
}

function ORMgetMenuHabilitado ($idMenu, $idRol) {
    $sql = "SELECT COUNT(*) as cant FROM roles_menu WHERE idMenu = '".$idMenu."' AND idRol = '".$idRol."'";
    $r = mysql_query($sql) or die(mysql_error());
    $arr = mysql_fetch_assoc($r);
    
    if($arr['cant'] == 0) {
        return false;
    } else {
        return true;
    }
}

function ORMAgregarAlMenu($idMenu, $idRol) {
    $sql = "INSERT INTO roles_menu (idMenu, idRol) VALUES ('".$idMenu."', '".$idRol."')";
    $r = mysql_query($sql) or die(mysql_error());
    
}

function ORMeliminarDelMenu($idMenu, $idRol) {
    $sql = "DELETE FROM roles_menu WHERE idMenu = '".$idMenu."' AND idRol = '".$idRol."'";
    $r = mysql_query($sql) or die(mysql_error());
    
}


?>
